package com.netx.generics.basic;
import com.netx.generics.collections.ImmutableList;
import com.netx.generics.util.Strings;

import java.io.StringWriter;
import java.util.List;


public class ErrorList {
	
	private final String _errorsHeader;
	private final ImmutableList<String> _errors;
	private final String _warningsHeader;
	private final ImmutableList<String> _warnings;

	public ErrorList(String eh, List<String> errors, String wh, List<String> warnings) {
		if(errors != null && errors.isEmpty()) {
			errors = null;
			eh = null;
		}
		if(warnings != null && warnings.isEmpty()) {
			warnings = null;
			wh = null;
		}
		if(errors == null && warnings == null) {
			throw new IllegalUsageException("both 'errors' and 'warnings' are null or empty");
		}
		// Errors:
		if(errors != null) {
			if(Strings.isEmpty(eh)) {
				throw new IllegalUsageException("null or empty errors header for non-empty errors list");
			}
			_errorsHeader = eh;
			_errors = new ImmutableList<String>(errors);
		}
		else {
			_errorsHeader = null;
			_errors = null;
		}
		// Warnings:
		if(warnings != null) {
			if(Strings.isEmpty(wh)) {
				throw new IllegalUsageException("null or empty warnings header for non-empty warnings list");
			}
			_warningsHeader = wh;
			_warnings = new ImmutableList<String>(warnings);
		}
		else {
			_warningsHeader = null;
			_warnings = null;
		}
	}
	
	public ErrorList(String eh, List<String> errors) {
		this(eh, errors, null, null);
	}

	public String getErrorsHeader() {
		return _errorsHeader;
	}

	public ImmutableList<String> getErrors() {
		return _errors;
	}
	
	public String getWarningsHeader() {
		return _warningsHeader;
	}

	public ImmutableList<String> getWarnings() {
		return _warnings;
	}
	
	public String toString() {
		StringWriter writer = new StringWriter();
		new ErrorListPrinter(writer).print(this);
		return writer.toString();
	}
}
